/**
 * May 22, 2014 2:13:26 PM
 */
package elitech.vietnam.copyweatherfashion.location;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.Locale;

import elitech.vietnam.copyweatherfashion.config.Const;

/**
 * @author Cong
 */
public class Geocoding {

	private static final String	APIURL	= "https://maps.googleapis.com/maps/api/geocode/json?latlng=%s,%s&sensor=false&language=%s&key=%s";

	public static String reverseGeocoder(double lat, double lon, Locale locale) throws IOException {
		InputStream is = new URL(String.format(APIURL, String.valueOf(lat), String.valueOf(lon), locale.getLanguage(), Const.GOOGLEAPI_KEY)).openStream();
		
		BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
		StringBuilder sb = new StringBuilder();
		int cp;
		while ((cp = rd.read()) != -1) {
			sb.append((char) cp);
		}
		String json = sb.toString();
		is.close();
		return json;
	}
}
